package com.example.mvvmdemo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.mvvmdemo.R;
import com.example.mvvmdemo.databinding.ActivityMainBinding;
import com.example.mvvmdemo.activity.litepal.DataLitepalActivity;
import com.example.mvvmdemo.activity.room.DataRoomActivity;
import com.example.mvvmdemo.activity.mtv.DataListActivity;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setLifecycleOwner(this);
        binding.setHandler(this);
        initViewModel();
        initView();

//        checkReadAndWritePermission();
    }

    private void checkReadAndWritePermission(){
        XXPermissions.with(this)
//                .permission(Permission.WRITE_EXTERNAL_STORAGE)
//                .permission(Permission.READ_EXTERNAL_STORAGE)
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_MEDIA_VIDEO)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(new OnPermissionCallback() {
                    @Override
                    public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                        if (!allGranted) {
                            ToastUtils.showShort("获取部分权限成功，但部分权限未正常授予");
                            return;
                        }
                        ToastUtils.showShort("获取权限成功");
                    }

                    @Override
                    public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                        OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
                        if (doNotAskAgain) {
                            ToastUtils.showShort("被永久拒绝授权，请手动授予权限");
                            // 如果是被永久拒绝就跳转到应用权限系统设置页面
                            XXPermissions.startPermissionActivity(getApplicationContext(), permissions);
                        } else {
                            ToastUtils.showShort("获取权限失败");
                        }
                    }
                });
    }

    private void initViewModel(){

    }

    private void initView(){

    }

    public void toDataListActivity(){
        ActivityUtils.startActivity(this, DataListActivity.class);
    }
    public void toDataRoomActivity(){
        ActivityUtils.startActivity(this, DataRoomActivity.class);
    }
    public void toDataLitepalActivity(){
        ActivityUtils.startActivity(this, DataLitepalActivity.class);
    }
}